/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import bean.Customer;
import bean.connectSQL;
import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ADMIN
 */
public class SqlConnect {
    Connection con;
    
    public SqlConnect() {
        try {
            String dbClass = "com.mysql.jdbc.Driver";
            String Url = "jdbc:mysql://localhost/banhang";
            Class.forName(dbClass);
            con = DriverManager.getConnection(Url,"root","");
            
        } catch (SQLException ex) {
            Logger.getLogger(SqlConnect.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SqlConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    public void  closeConnection(){
        try{
        con.close();
        }catch(Exception e){
            
        }
    }
    public Customer getCustomer(String username, String password){
        
        
        try {
            String query = "select * from user_cus where username = ? and password = ?";
            Customer customer = new Customer();
            PreparedStatement ps = (PreparedStatement) con.prepareStatement(query);
             ps.setString(1,username);
             ps.setString(2,password);
            
            ResultSet rs = ps.executeQuery();
            
            if(rs.next()){
                customer.setName(rs.getString("name"));
                customer.setUsername(rs.getString("username"));
                customer.setPassword(rs.getString("password"));
                customer.setEmail(rs.getString("email"));
                customer.setAddress(rs.getString("address"));;
                
                return customer;
            }
        } catch (SQLException ex) {
            System.out.println("ngu qua");
            Logger.getLogger(connectSQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;    
    }
    public int insertCustomer(Customer customer){
        try{
           
            String querySQL = "Insert into user_cus(username,password,name,email,address) values(?,?,?,?,?)";
            PreparedStatement pre = (PreparedStatement) con.prepareStatement(querySQL);
            pre.setString(1, customer.getUsername());
            pre.setString(2, customer.getPassword());
            pre.setString(3, customer.getName());
            pre.setString(4, customer.getEmail());
            pre.setString(5, customer.getAddress());
            return  pre.executeUpdate();
         
        }catch(Exception e){
            Logger.getLogger(e.getMessage());
            System.out.println("hic hic lai co loi rui");
        }
        return -1;
    }
    
    
}
